let { addProducts, getProducts, deleteProducts } = require('../dao/productsDao.js');

//图片处理包
const { moveFiles, removeFiles } = require("../utils/handleFiles");
//获取产品数据
module.exports.getProducts = async (params) => {
  let data = await getProducts(params);
  return {
    code: 1,
    message: '保险产品数据获取成功',
    data
  }
}

//新增产品数据
module.exports.addProducts = async function (params) {
  console.log(222,params);
  let data = await addProducts(params);
  if (data._id) {
    // 移动图片
    moveFiles({
      fromPath: './public/temp',
      toPath: './public/images',
      filename: params.productsPic,
    });
    //删除缓存图片
    removeFiles('./public/temp')
    return {
      message: "新增成功",
      code: 1,
      data
    }
  } return {
    message: "新增失败",
    code: 0
  }
};
//删除数据
module.exports.deleteProducts = async (_id) => {
  const data = await deleteProducts(_id);
  console.log(222, data.n, data.ok, data.deletedCount);
  if (data.n > 0) {
    return {
      message: '删除成功',
      code: 1
    }
  } return {
    message: '删除失败',
    code: 0
  }
}